package com.fulihui.systemcore.service;

import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.near.servicesupport.request.TPageRequest;
import org.near.servicesupport.request.TRequest;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.result.TMultiResult;
import org.near.servicesupport.result.TPageResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.servicesupport.util.ConditionValidator;
import org.springframework.stereotype.Component;

import com.fulihui.systemcore.api.ProjectService;
import com.fulihui.systemcore.dto.ProjectDTO;
import com.fulihui.systemcore.enums.CommonStatusEnum;
import com.fulihui.systemcore.repository.ProjectRepository;
import com.fulihui.systemcore.request.project.ProjectEditRequest;
import com.fulihui.systemcore.request.project.ProjectQueryPageRequest;
import com.fulihui.systemcore.request.project.ProjectQueryRequest;
import com.fulihui.systemcore.request.project.QueryByCodeRequest;
import com.fulihui.systemcore.response.project.QueryResponse;
import com.fulihui.systemcore.util.PinYinUtil;

/**
 * @author leeson 2016年5月17日 下午3:17:18
 *
 */
@Component("projectService")
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectRepository projectRepository;

    @Override
    public TSingleResult<QueryResponse> queryByCode(TRequest<QueryByCodeRequest> request) {
        ConditionValidator.validate(request.getCondition());

        ProjectDTO project = projectRepository.queryByCode(request.getCondition().getProjectCode());

        QueryResponse response = new QueryResponse();
        response.setProject(project);

        return ResultBuilder.succTSingle(response);
    }

    @Override
    public BaseResult create(TRequest<ProjectEditRequest> request) {
        ProjectEditRequest condition = request.getCondition();

        ConditionValidator.validate(condition);

        if (null == condition || StringUtils.isEmpty(condition.getName())) {
            return ResultBuilder.fail(BaseResult.class, 101, "parameter is missing.");
        }

        String projectCode = PinYinUtil.toPinYin(condition.getName());
        if (null == projectCode) {
            return ResultBuilder.fail(BaseResult.class, 102, "[name] include special characters.");
        }

        //确保所有都转为大写
        projectCode = projectCode.toUpperCase();

        ProjectDTO projectDTO = new ProjectDTO();
        projectDTO.setProjectCode(projectCode);
        projectDTO.setName(condition.getName());
        projectDTO.setLink(condition.getLink());
        projectDTO.setStatus(CommonStatusEnum.ENABLE.getCode());

        //插入数据
        try {
            projectRepository.insert(projectDTO);
        } catch (Exception e) {
            return ResultBuilder.fail(BaseResult.class, 103, "项目名称已存在.");
        }

        return ResultBuilder.succ(BaseResult.class);
    }

    @Override
    public BaseResult update(TRequest<ProjectEditRequest> request) {
        ProjectEditRequest condition = request.getCondition();

        ConditionValidator.validate(condition);

        if (null == condition || null == condition.getProjectId()) {
            return ResultBuilder.fail(BaseResult.class, 101, "parameter is missing.");
        }

        ProjectDTO projectDTO = new ProjectDTO();
        projectDTO.setId(condition.getProjectId());
        projectDTO.setName(condition.getName());
        projectDTO.setLink(condition.getLink());
        projectDTO.setStatus(condition.getStatus());

        //更新数据
        try {
            projectRepository.update(projectDTO);
        } catch (Exception e) {
            return ResultBuilder.fail(BaseResult.class, 102, "项目名称已存在.");
        }

        return ResultBuilder.succ(BaseResult.class);
    }

    @Override
    public TPageResult<ProjectDTO> queryPage(TPageRequest<ProjectQueryPageRequest> request) {
        ProjectQueryPageRequest condition = request.getCondition();

        ConditionValidator.validate(condition);

        if (null == condition) {
            return ResultBuilder.failTPage(101, "parameter is missing.");
        }
        ProjectDTO record = new ProjectDTO();
        record.setName(condition.getName());
        record.setProjectCode(condition.getProjectCode());
        record.setStatus(condition.getStatus());

        List<ProjectDTO> projectList = projectRepository.queryPage(record, request.start4Mysql(),
            request.getRows());
        int totalCount = projectRepository.queryPageCount(record);

        return ResultBuilder.succTPage(projectList, request.getPage(), request.getRows(),
            totalCount);
    }

    @Override
    public TMultiResult<ProjectDTO> query(TRequest<ProjectQueryRequest> request) {
        ProjectQueryRequest condition = request.getCondition();

        ConditionValidator.validate(condition);

        if (null == condition) {
            return ResultBuilder.failTMulti(101, "parameter is missing.");
        }

        ProjectDTO record = new ProjectDTO();
        record.setName(condition.getName());
        record.setProjectCode(condition.getProjectCode());
        record.setStatus(condition.getStatus());
        record.setId(condition.getProjectId());

        List<ProjectDTO> projectList = projectRepository.query(record);

        return ResultBuilder.succTMulti(projectList);
    }
}